#!/usr/bin/env python
# coding=utf-8
"""
@Author  : youjia - 卞志伟
@file    : day_report_business.py
@contact : bianzhiwei@iyoujia.com
@time    : 2019-08-12 15:18 
@Desc    : 
@Software: PyCharm
"""
import os
import sys

# 当前文件的路径
pwd, filename = os.path.split(os.path.abspath(__file__))
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
sys.path.append(pwd)
sys.path.append(father_path)
sys.path.append(grader_father)

from report_system.utils import excel_util
from report_system.utils import mail_util
from report_system.utils import mailbox_util
from report_system.utils.enum_util import DAY_REPORT_BUSINESS_COLOR
from report_system.utils.channel_util import TUJIA, ZHENGUO, AIRBNB
from report_system.report_specify.business_specify import *
from report_system.report_specify.service_specify import house_refund_rate_month
from report_system.utils.development_util import debug

if debug:
    receivers = 'dt@iyoujia.com;wujiming@iyoujia.com'
    city_mails = mailbox_util.mails_test
else:
    receivers = 'yitangf@sweetome.com;yangweiwei@iyoujia.com;machao@iyoujia.com;' \
                'wangjing@iyoujia.com;wangjian@iyoujia.com;dt@iyoujia.com;wangchao@iyoujia.com;' \
                'yy@iyoujia.com;ccsj@iyoujia.com;zhuqiang@iyoujia.com;lvweixin@iyoujia.com'
    city_mails = mailbox_util.mails_all


def process(st=None, et=None):
    yesterday = date_util.curdate()
    st = st if st else date_util.cur_month_first(days=1, dt=yesterday)
    et = et if et else yesterday

    house_base_df = house_base_data()
    house_base_df = df_util.df_set_index(house_base_df, ["房屋ID", "城市"])
    house_base_df = df_util.df_set_first_title(house_base_df, "基础数据")
    log.info("房屋基础数据准备完毕！")

    house_rent_day_df = house_rent_day(dt=et)
    house_rent_day_df = df_util.df_set_index(house_rent_day_df, "房屋ID")
    log.info("未来可租、已售间夜准备完毕！")

    house_price_df = house_price(dt=et)
    house_price_df = df_util.df_set_index(house_price_df, "房屋ID")
    log.info("未来各渠道价格准备完毕！")

    day_df = pd.merge(house_price_df, house_rent_day_df, how='left', left_index=True, right_index=True)

    day_df = df_util.df_set_first_title(day_df, "今日数据")

    result_df = pd.merge(house_base_df, day_df, how='left', left_index=True, right_index=True)

    house_sale_data_df = house_sale_data(st=st, et=et)
    sale_data_df = df_util.df_set_index(house_sale_data_df, "房屋ID")

    house_refund_rate_month_df = house_refund_rate_month(st=st, et=et)
    house_refund_rate_month_df = df_util.df_set_index(house_refund_rate_month_df, "房屋ID")

    total_df = pd.merge(sale_data_df, house_refund_rate_month_df, how='left', left_index=True, right_index=True)
    total_df = df_util.df_set_first_title(total_df, "累计数据")
    result_df = pd.merge(result_df, total_df, how='left', left_index=True, right_index=True)

    tujia_order_df = channel_order(st=st, et=et, channel_id=TUJIA)
    tujia_tag_df = tujia_tag(dt=et)
    tujia_df = pd.merge(tujia_order_df, tujia_tag_df, how='outer')
    tujia_df = df_util.df_set_index(tujia_df, "房屋ID")
    tujia_df = df_util.df_set_first_title(tujia_df, "途家")
    result_df = pd.merge(result_df, tujia_df, how='left', left_index=True, right_index=True)

    zhenguo_order_df = channel_order(st=st, et=et, channel_id=ZHENGUO)
    zhenguo_tag_df = zhenguo_tag(dt=et)
    zhenguo_df = pd.merge(zhenguo_order_df, zhenguo_tag_df, how='outer')
    zhenguo_df = df_util.df_set_index(zhenguo_df, "房屋ID")
    zhenguo_df = df_util.df_set_first_title(zhenguo_df, "榛果")
    result_df = pd.merge(result_df, zhenguo_df, how='left', left_index=True, right_index=True)

    airbnb_order_df = channel_order(st=st, et=et, channel_id=AIRBNB)
    airbnb_tag_df = airbnb_tag(dt=et)
    airbnb_df = pd.merge(airbnb_order_df, airbnb_tag_df, how='outer')
    airbnb_df = df_util.df_set_index(airbnb_df, "房屋ID")
    airbnb_df = df_util.df_set_first_title(airbnb_df, "爱彼迎")
    result_df = pd.merge(result_df, airbnb_df, how='left', left_index=True, right_index=True)
    # result_df = result_df.reset_index()
    # result_df = df_util.df_set_index(result_df, '城市')

    df_dict = {'经营日报-房屋': result_df}
    colors_dict = {'经营日报-房屋': DAY_REPORT_BUSINESS_COLOR}
    #
    text = """    Dear All:
                    附件是经营日报！
                    请查收！
                    谢谢！
            如有问题！
            请联系: dt@iyoujia.com
            """
    mail_util.distribute_mail(df_dict=df_dict, file_name='经营日报-房屋', text=text, engine='xlsxwriter', #  html_sheet_name='经营日报',
                              title="【经营日报-房屋】", colors_dict=colors_dict, receiver=receivers, na_rep='-',
                               uuid='city', freeze_panes=(2, 8))

    # excel_util.pd_to_excel({'result_df': result_df}, '00订单', engine='xlsxwriter', merge_cells=True)


if __name__ == '__main__':
    process()
